package com.example.project.projectspringcloud.entity;

import com.example.project.projectspringcloud.utils.JsonUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class RespBody {

    public static final int OK_CODE = 200;
    public static final int ERROR_CODE = -1;
    public static final int ERROR_VALIDATE_CODE = -2;

    private static final RespBody OK = new RespBody(OK_CODE, null, null);
    private static final RespBody ERROR = new RespBody(ERROR_CODE, null, null);

    private int status;

    private Object data;

    private String error;

    public static RespBody ok(Object data) {
        return new RespBody(OK_CODE, data, null);
    }

    public static RespBody error(String error) {
        return new RespBody(ERROR_CODE, null, error);
    }

    public static RespBody error(int errorCode, String error) {
        return new RespBody(errorCode, null, error);
    }

    public static RespBody error(int errorCode, Object error) {
        return new RespBody(errorCode, error, "");
    }

    @Override
    public String toString() {
        return "{" + "\"status\":" + status + ",\"data\":" + JsonUtil.obj2Json(data) + ",\"error\":\""
                + error + "\",\"version\":" + System.currentTimeMillis() + '}';
    }
}
